package com.pingba.serviceImpl.msg;

import com.pingba.dao.BaseDao;
import com.pingba.dao.msg.MsgSubscribeDao;
import com.pingba.entity.User;
import com.pingba.entity.msg.MsgSend;
import com.pingba.entity.msg.MsgSubscribe;
import com.pingba.service.msg.MsgSubscribeService;
import com.pingba.serviceImpl.BaseServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/28 0028.
 */
@Service("MsgSubscribeService")
public class MsgSubscribeServiceImpl extends BaseServiceImpl<MsgSubscribe, Long> implements MsgSubscribeService{

    @Autowired
    private MsgSubscribeDao msgSubscribeDao;

    private static Logger logger = Logger.getLogger(MsgSubscribeServiceImpl.class);

    @Override
    public BaseDao<MsgSubscribe, Long> getDao() {
        return msgSubscribeDao;
    }

    /**
     * 获取该所有的订阅消息
     * @param user
     * @return
     */
    @Override
    public List<MsgSend> mySubscribes(User user) {
        if (user == null) {
            return new ArrayList<MsgSend>();
        }
        return msgSubscribeDao.mySubscribes(user);
    }

    /**
     * 将该条消息置为删除状态
     * @param subscribe
     */
    @Override
    public void setDeleted(MsgSubscribe subscribe) {
        msgSubscribeDao.setDeleted(subscribe);
    }

    @Override
    public void unSubscribeOne(long userId, long msgSendId) {
        msgSubscribeDao.unSubscribeOne(userId, msgSendId);
    }

    @Override
    public void subscribeOne(MsgSubscribe subscribe) {
        if (!isSubsribed(subscribe.getUserId(), subscribe.getMsgId())) {
            msgSubscribeDao.insert(subscribe);
        }
    }

    @Override
    public void subscribeOne(long userId, long msgSendId) {
        if (isSubsribed(userId, msgSendId)) {
            logger.info("userId: " + userId + "  has subscribe the msgSend of id: " + msgSendId);
            return;
        }
        MsgSubscribe msgSubscribe = MsgSubscribe.getOne(userId, msgSendId);
        msgSubscribeDao.insert(msgSubscribe);
    }

    @Override
    public boolean isSubsribed(long userId, long msgSendId) {
        List<MsgSubscribe> subscribeList = msgSubscribeDao.isSubsribed(userId, msgSendId);
        return subscribeList.size() > 0;
    }

    @Override
    public void deleteSubscribeOfSend(long msgSendId) {
        msgSubscribeDao.deleteSubscribeOfSend(msgSendId);
    }
}
